﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _4_10
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListBox1.Items.Add("Administrator");
                ListBox1.Items.Add("Guest");
                ListBox1.Items.Add("小王");
                ListBox1.Items.Add("小刘");
                ListBox1.Items.Add("小李");
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //获取ListBox1的列表数
            int count = ListBox1.Items.Count;
            //循环将ListBox1中的数据转移到ListBox2中
            for (int i = 0; i < count; i++)
            {
                ListItem Item = ListBox1.Items[0];
                ListBox1.Items.Remove(Item);
                ListBox2.Items.Add(Item);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int count = ListBox2.Items.Count;
            //循环将ListBox2中的数据转移到ListBox1中
            for (int i = 0; i < count; i++)
            {
                ListItem Item = ListBox2.Items[0];
                ListBox2.Items.Remove(Item);
                ListBox1.Items.Add(Item);
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int count = ListBox1.Items.Count;
            int index = 0;
            //循环判断各个项的选中状态
            for (int i = 0; i < count; i++)
            {
                ListItem Item = ListBox1.Items[index];
                //如果选项为选中状态从ListBox1中删除并添加到ListBox2中
                if (ListBox1.Items[index].Selected == true)
                {
                    ListBox1.Items.Remove(Item);
                    ListBox2.Items.Add(Item);
                    //删除后回退
                    index--;
                }
                //获取下一个选项
                index++;
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            int count = ListBox2.Items.Count;
            int index = 0;
            for (int i = 0; i < count; i++)
            {
                ListItem Item = ListBox2.Items[index];
                if (ListBox2.Items[index].Selected == true)
                {
                    ListBox2.Items.Remove(Item);
                    ListBox1.Items.Add(Item);
                    index--;
                }
                index++;
            }
        }
    }
}